package com.bootdo.fx.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.common.utils.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.fx.domain.AdInfoDO;
import com.bootdo.fx.service.AdInfoService;

import javax.servlet.http.HttpServletResponse;

/**
 * 
 * 
 * @author 袁飞龙
 * @email 1992lcg@163.com
 * @date 2017-11-30 17:12:20
 */
 
@Controller
public class AdInfoController {
	@Autowired
	private AdInfoService adInfoService;
	
	@GetMapping("/fx/adInfo")
	@RequiresPermissions("fx:adInfo:adInfo")
	String AdInfo(){
	    return "fx/adInfo/adInfo";
	}
	
	@ResponseBody
	@GetMapping("/fx/adInfo/list")
	@RequiresPermissions("fx:adInfo:adInfo")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<AdInfoDO> adInfoList = adInfoService.list(query);
		int total = adInfoService.count(query);
		PageUtils pageUtils = new PageUtils(adInfoList, total);
		return pageUtils;
	}
	
	@GetMapping("/fx/adInfo/add")
	@RequiresPermissions("fx:adInfo:add")
	String add(){
	    return "fx/adInfo/add";
	}

	@GetMapping("/fx/adInfo/edit/{adId}")
	@RequiresPermissions("fx:adInfo:edit")
	String edit(@PathVariable("adId") Long adId,Model model){
		AdInfoDO adInfo = adInfoService.get(adId);
		model.addAttribute("adInfo", adInfo);
	    return "fx/adInfo/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/fx/adInfo/save")
	@RequiresPermissions("fx:adInfo:add")
	public R save( AdInfoDO adInfo){
		if(adInfoService.save(adInfo)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/fx/adInfo/update")
	@RequiresPermissions("fx:adInfo:edit")
	public R update( AdInfoDO adInfo){
		adInfoService.update(adInfo);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/fx/adInfo/remove")
	@ResponseBody
	@RequiresPermissions("fx:adInfo:remove")
	public R remove( Long adId){
		if(adInfoService.remove(adId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/fx/adInfo/batchRemove")
	@ResponseBody
	@RequiresPermissions("fx:adInfo:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] adIds){
		adInfoService.batchRemove(adIds);
		return R.ok();
	}


	/*
	* 获取广告列表
	* */
	@RequestMapping("/wx/adInfo/listAdInfos")
	@ResponseBody
	public JsonResult listAdInfos(HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		Map<String,Object> map = new HashMap<>();
		map.put("isEnable",1);
		return ResultUtil.renderSuccess(adInfoService.list(map));
	}

	/*
     * 点击广告详情
     * */
	@RequestMapping("/wx/adInfo/listAdInfoById")
	@ResponseBody
	public JsonResult listAdInfoById(String adId,HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		return ResultUtil.renderSuccess(adInfoService.get(Long.parseLong(adId)));
	}

	
}
